#include "graphsupport/graph/Graph.h"

#include "accumulsupport/AccumulSupport.h"

Graph::Graph( int vertexCount, AdjCoefType infEdge )
{
	init( vertexCount, infEdge );
}

void Graph::init( int vertexCount, AdjCoefType infEdge )
{
	if ( infEdge == -1 )
	{
		infEdge = this->infEdge;
	}
	this->vertexCount = vertexCount;
	accumf::reinit( adjBuffer, vertexCount * vertexCount, infEdge );
	accumf::reinit( hrdBuffer, vertexCount, (double*)0 );
	this->infEdge = infEdge;

	adjMatrix = & hrdBuffer[0];
	for ( int i = 0; i < vertexCount; i++ )
	{
		adjMatrix[i] = & adjBuffer[ vertexCount * i ];
	}
}

Graph::Graph( const Graph & that )
{
	this->operator = ( that );
}

void Graph::operator = ( const Graph & that )
{
	this->init( that.vertexCount, that.infEdge );
	for ( int i = 0; i < vertexCount; i++ )
	{
		for ( int j = 0; j < vertexCount; j++ )
		{
			this->adjMatrix[i][j] = that.adjMatrix[i][j];
		}
	}
}

Graph::~Graph()
{
}

void Graph::eraseEdge( int from, int to )
{
	adjMatrix[from][to] = infEdge;
}

void Graph::eraseAllEdges()
{
	for ( int i = 0; i < vertexCount; i++ )
	{
		for ( int j = 0; j < vertexCount; j++ )
		{
			eraseEdge( i, j );
		}
	}
}

int Graph::getVertexCount()
{
	return vertexCount;
}

Graph::AdjMatrixType Graph::getAdjacentMatrixArr()
{
	return adjMatrix;
}

bool Graph::existsEdge( int from, int to )
{
	if ( from >= vertexCount || to >= vertexCount )
	{
		throw "invalid vertex number";
	}
	return adjMatrix[from][to] < infEdge;
}

Graph::AdjCoefType Graph::getInfEdge()
{
	return infEdge;
}
